#pragma once
#include "../comm/log.hpp"
#include <string>
#include <unordered_map>
#include <assert.h>
#include <vector>
#include <fstream>
#include "../comm/util.hpp"
#include <stdlib.h>
#include "../comm/log.hpp"
#include <mysql/mysql.h>

// 题目加载到内存
namespace ns_mode1
{
    using namespace std;
    using namespace ns_util;
    using namespace ns_log;
    struct Question
    {

        std::string number; // 编号
        std::string title;  // 题目标题
        std::string star;   // 级别
        int cpu_limit;      // 时间
        int mem_limit;      // 内存
        std::string desc;   // 题目描述
        std::string header; // 题目预设
        std::string tail;   // 测试用例
    };
    std::string oj_questions = "oj_questions";

    const std::string host = "127.0.0.1";
    const std::string user = "oj_client";
    const std::string password = "1234";
    const std::string db = "oj";
    const int port = 3306;
    class Models
    {

    public:
        Models()
        {
        }
        ~Models() {}
        bool QueryMySql(const std::string &sql, vector<Question> *out)
        {
            MYSQL *my = mysql_init(nullptr);
            if (nullptr == mysql_real_connect(my, host.c_str(), user.c_str(), password.c_str(), db.c_str(), port, nullptr, 0))
            {
                LOG(FATAL) << "数据库连接失败！！！" << "\n";
                return false;
            }
            mysql_set_character_set(my, "utf8");
            LOG(FATAL) << "MYSQL数据库连接成功！！！" << "\n";

            if (0 != mysql_query(my, sql.c_str()))
            {
                LOG(WARNING)<<"执行语句失败："<<sql<< "\n";
                return false;
            }
          
            MYSQL_RES *res = mysql_store_result(my);
          
            int rows = mysql_num_rows(res);
            int cols = mysql_num_fields(res);

            Question q;
           
           
            for (int i = 0; i < rows; i++)
            {
                MYSQL_ROW row = mysql_fetch_row(res);
                q.number = row[0];
                q.title = row[1];
                q.star = row[2];
                q.desc = row[3];
                q.header = row[4];
                q.tail = row[5];
                q.cpu_limit = atoi(row[6]);
                q.mem_limit = atoi(row[7]);
                out->push_back(q);
    
            }

            return true;
        }
        bool GetAllQuestions(vector<Question> *out)
        {
            bool ret = true;
            std::string sql = "select * from ";
            sql += oj_questions;
            return QueryMySql(sql, out);
        }
        bool GetOneQuestions(const std::string &number, Question *out)
        {
            bool ret = false;
            std::string sql = "select * from ";
            sql += oj_questions;
            sql += " where number=";
            sql += number;
            vector<Question> result;
            if (QueryMySql(sql, &result))
            {
                if (result.size() == 1)
                {
                    *out = result[0];
                    ret = true;
                }
            }
            return ret;
            ;
        }
    };

}